/* Author DSL */
import java.awt.Color;

import java.io.File;
import java.util.Scanner;

import java.io.InputStream;
import java.io.FileOutputStream;
        
import java.util.ArrayList;

import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.Enumeration;


import java.io.FileNotFoundException;
import java.io.IOException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author daniel
 */
public class LoginHandler {
    
    private static File outputDir;
    protected static String programJarFileName = "DolphinSystemInformation_DSL_backup_new.jar";
 
    
    private static void makeLogedinForm_coach(String inputLogin, String inputPassword, String role){
    CoachGUI LogedinForm = new CoachGUI(inputLogin, inputPassword, role);
    LogedinForm.setVisible(true);
    }
    
    private static void makeLogedinForm_ct(String inputLogin, String inputPassword, String role){
    TreasureGUI LogedinForm = new TreasureGUI(inputLogin, inputPassword, role);
    LogedinForm.setVisible(true);
    }
    
    private static void makeLogedinForm_cc(String inputLogin, String inputPassword, String role){
    ChairmanGUI LogedinForm = new ChairmanGUI(inputLogin, inputPassword, role);
    LogedinForm.setVisible(true);
    }
    
    public static void verifyCredentials(String inputLogin, char [] inPassArray, DIS_LoginGUI loginForm, javax.swing.JButton LoginButton, javax.swing.JLabel Status, File inputFile) throws FileNotFoundException, java.io.IOException{
    
        String inputPassword = "";

        
        for(int i =0;i < inPassArray.length;i++){inputPassword = inputPassword + inPassArray[i];}
        
        try{
            ArrayList<String> logins = new ArrayList(getLoginCredentials(inputFile.getName()));
            
                for(int i=0; i < logins.size(); i++){
                  if(inputFile.exists()){

                    if(inputLogin.matches(logins.get(i)) 
                    && inputPassword.matches(logins.get(i+1)) && logins.get(i+2).matches("coach")){
                    makeLogedinForm_coach(inputLogin, inputPassword, logins.get(i+2));
                    loginForm.setVisible(false);
                    }
                    else if(inputLogin.matches(logins.get(i)) 
                    && inputPassword.matches(logins.get(i+1)) && logins.get(i+2).matches("ct")){
                    makeLogedinForm_ct(inputLogin, inputPassword, logins.get(i+2));
                    loginForm.setVisible(false);
                    }
                    else if(inputLogin.matches(logins.get(i)) 
                    && inputPassword.matches(logins.get(i+1)) && logins.get(i+2).matches("cc")){
                    makeLogedinForm_cc(inputLogin, inputPassword, logins.get(i+2));
                    loginForm.setVisible(false);
                    }
                    else{Status.setForeground(Color.RED);
                    Status.setText("Credentials are invalid!");}
                    } // end outer if.

                  
                  }// end for.
                if(!inputFile.exists()){
                    LoginButton.setText("relogin");
                    
                    Status.setText(inputFile.getName()+" was not found! Extracting file...");
                    extractFileFromJar(programJarFileName,"",inputFile.getName());
                    Status.setText("Done processing: " + inputFile.getName()+" please try to login again.");
                 
                      } // end if.
                
         } // end try.
            catch (java.io.FileNotFoundException ex){
                    ex.printStackTrace();
                    Status.setForeground(Color.BLUE);
                    Status.setText(inputFile.getName()+" was not found! (FileNotFoundException)");
            }
            catch (java.io.IOException ex){ex.printStackTrace();
            Status.setForeground(Color.BLUE);
            Status.setText(inputFile.getName()+" was not found! (IOException)");
            }
    
    
    } // end verifyCredentials.
    
    public static ArrayList getLoginCredentials(String fileName) throws FileNotFoundException{
    
        ArrayList inputList = new ArrayList();
        
                    try{
                        
                        Scanner infile = new Scanner(new File(fileName));
                        infile.useDelimiter(",");
                        
                        while(infile.hasNext()){
                        // reads in the next token from the Scanner obj and trims the white spaces from it.    
                        inputList.add(infile.next().trim());
                        
                        
			System.out.println("Entry added!");
                        } // end while.
                        infile.close();
                    }
                    catch (java.io.FileNotFoundException ex){
                    ex.printStackTrace();
                    }
                        System.out.println(inputList+"["+inputList.size()+"]");
                        return inputList;
    
    }// end getLoginCredentials.

    public static void extractFileFromJar(String jarFileToExtractFrom, String destDir, String fileName) throws IOException{
        
        
        try{
        JarFile jar = new JarFile(jarFileToExtractFrom);
        Enumeration enumVar = jar.entries();
                while (enumVar.hasMoreElements()) {
                JarEntry file = (JarEntry) enumVar.nextElement();
                if(file.getName().equalsIgnoreCase(fileName)){
                    if(destDir.equalsIgnoreCase("")){
                    outputDir = new File(file.getName());
                    }
                    else{outputDir = new File(destDir + File.separator + file.getName());}
                    if (file.isDirectory()) { // if its a directory, create it
                    outputDir.mkdir();
                    continue;
                    }
                    InputStream fileInputStream = jar.getInputStream(file); // get the input stream
                    FileOutputStream fileOutputStream = new FileOutputStream(outputDir);
                        while (fileInputStream.available() > 0) {  // write contents of 'fis' to 'fos'
                        fileOutputStream.write(fileInputStream.read());
                        }
                fileOutputStream.close();
                fileInputStream.close();
                } // end outer if.
                
                } // end while.
        
        
            } // end try.
            catch (java.io.IOException ex){ex.printStackTrace();} // end catch IOException.
        
    } // end extractFileFromJar.
    
    
    
} 